/*
 * @Author: master
 * @Date: 2022-03-29 16:25:50
 * @LastEditTime: 2022-04-01 15:45:04
 * @Description: 国际化
 */
import { createI18n } from 'vue-i18n';
import http from '@/common/http/http.js';
const i18n = createI18n({
  globalInjection: true, // 全局注册
  legacy: false, // Composition API 模式
  locale: 'zh-CN',
  messages: {},
});
/**
 * 获取本地国际化语言
 */
http(`${viteConfig.base}static/locale/locale.json`)
  .then(async res => {
    if (res?.length) {
      const locale = i18n.global.locale.value;
      const curLocale = res.filter(item => item.key === locale)[0];
      if (curLocale) {
        const path =
          curLocale.path.indexOf('http') === -1 ? `${viteConfig.base}static/locale/${curLocale.path}` : curLocale.path;
        const data = await http(path);
        i18n.global.setLocaleMessage(curLocale.key, data);
      }
    }
  })
  .catch(e => {
    console.error(e);
  });
export default i18n;
